達夢數(shù)據(jù)庫dm8與PYthon連接訪問
發(fā)布時間:2024/9/12 14:27:42
達夢數(shù)據(jù)庫
===================
主機名LOCALHOST
賬號名
SYSDBA
登錄密碼
123456789
數(shù)據(jù)庫名DAMENG
實例名DMSERVER
端口5236
=====================
Pycharm默認沒有連接達夢的驅動,首先選擇給Pycharm添加一個達夢的驅動。
找到自己電腦上達夢的JDBC驅動,因為我電腦上安裝了達夢數(shù)據(jù)庫,所有的驅動文件都在他的安裝目錄里。
通過驅動方式選擇創(chuàng)建數(shù)據(jù)庫連接。
SYSDBA
123456789
jdbc:dm://127.0.0.1:5236?schema=PRODUCTION
//====python 配置文件===================================
import dmPython
conn = dmPython.connect(user='SYSDBA', password='123456789',
server='localhost', port=5236)
//======python 調用頁面=================================
#!/usr/bin/python
# coding:utf-8
import dmPython
from apps.damengDB import conn
import json
try:
cursor = conn.cursor()
print('python: conn success!')
try:
# 清空表,初始化測試環(huán)境
cursor.execute('delete from PRODUCTION.PRODUCT_CATEGORY')
print('python: delete success!')
except (dmPython.Error, Exception) as err:
print(err)
try:
# 插入數(shù)據(jù)
cursor.execute("insert into PRODUCTION.PRODUCT_CATEGORY(NAME) values('88888'), ('66666')")
print('python: insert success!')
# 刪除數(shù)據(jù)
# cursor.execute("delete from PRODUCTION.PRODUCT_CATEGORY where name='Go'")
# print('python: delete success!')
#
# # 更新數(shù)據(jù)
# cursor.execute('update PRODUCTION.PRODUCT_CATEGORY set name = \'C++\' where name=\'C\'')
# print('python: update success!')
#
# # 查詢數(shù)據(jù)
# cursor.execute("select name from PRODUCTION.PRODUCT_CATEGORY")
cursor.execute("call mytest_proc(parm_OPType=>101);")
rows = cursor.fetchall()
# 列名
columns = [column[0] for column in cursor.description]
# 將獲取的數(shù)據(jù)轉換為字典列表
results = []
for row in rows:
result = dict(zip(columns, row))
results.append(result)
print("字典")
print(results)
for tmp in results:
print(tmp["NAME"])
# 將字典列表轉換為JSON格式的字符串
json_data = json.dumps(results)
# 輸出JSON數(shù)據(jù)
print("輸出JSON數(shù)據(jù)")
print(json_data)
# for tmp in json_data:
# print(tmp)
# =======================================
# # 將結果轉換為列表的字典
# results = []
# for row in rows:
# results.append(dict(zip(cursor.description, row)))
#
# # 將列表轉換為JSON字符串
# json_data = json.dumps(results)
#
# # 輸出JSON字符串
# print(json_data)
# == == == == == == == == == == == == == == == == == == == =
# for tmp in res:
# print(tmp)
# print('-----------')
# print(tmp[0])
# print(tmp[1])
# # for c1 in tmp:
# # print(c1)
print('python: select success!')
except (dmPython.Error, Exception) as err:
print(err)
conn.close()
except (dmPython.Error, Exception) as err:
print(err)
//=================================
pip install Flask Flask-SQLAlchemy pymssql